package org.iabako.server.taglib;

import org.iabako.shared.entity.enumeration.Language;
import org.iabako.shared.entity.enumeration.WebsiteDescriptionSection;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * Created by jose on 29/01/15.
 */
public class GoogleExposedLinksTag extends AbstractTag {

    public int doStartTag() throws JspException {

        JspWriter out = pageContext.getOut();

        WebsiteDescriptionSection section = (WebsiteDescriptionSection) pageContext.getSession().getAttribute("IABAKO_DESCRIPTION_SECTION");
        section = section == null ? WebsiteDescriptionSection.main : section;

        Language pickedLang = getPickedLanguage((HttpServletRequest) pageContext.getRequest());

        String msgFilePrefix = "/org/iabako/i18n/";
        try {
            InputStream is = this.getClass().getResourceAsStream(msgFilePrefix + pickedLang.getDescriptionFileName());
            BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String line;
            String anchorMainTextValue = null;
            String anchorMainAltValue = null;
            String anchorWhatTextValue = null;
            String anchorWhatAltValue = null;
            String anchorStartTextValue = null;
            String anchorStartAltValue = null;

            while ((line = br.readLine()) != null) {
                if (line.contains("website_title_main=")) {
                    anchorMainAltValue = line.replace("website_title_main=", "");
                } else if (line.contains("website_description_main=")) {
                    anchorMainTextValue = line.replace("website_description_main=", "");
                } else if (line.contains("website_title_what=")) {
                    anchorWhatTextValue = line.replace("website_title_what=", "");
                } else if (line.contains("website_description_what=")) {
                    anchorWhatAltValue = line.replace("website_description_what=", "");
                } else if (line.contains("website_title_start=")) {
                    anchorStartTextValue = line.replace("website_title_start=", "");
                } else if (line.contains("website_description_start=")) {
                    anchorStartAltValue = line.replace("website_description_start=", "");
                }
            }

            String mainURL = "/description/main/" + pickedLang.name();
            String whatURL = "/description/what/" + pickedLang.name();
            String startURL = "/description/start/" + pickedLang.name();

            if (section == WebsiteDescriptionSection.what || section == WebsiteDescriptionSection.start) {
                out.println("<a style=\"display:none\" href=\"" + mainURL + "\" alt=\"" + anchorMainAltValue + "\">" + anchorMainTextValue + "</a>");
            } else {
                out.println("<a style=\"display:none\" href=\"" + whatURL + "\" alt=\"" + anchorWhatAltValue + "\">" + anchorWhatTextValue + "</a>");
                out.println("<a style=\"display:none\" href=\"" + startURL + "\" alt=\"" + anchorStartAltValue + "\">" + anchorStartTextValue + "</a>");
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        pageContext.getSession().removeAttribute("IABAKO_DESCRIPTION_SECTION");

        return super.doStartTag();
    }

}
